package com.source.diyjuc;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @Author wengqy
 * @Date 2021/1/20
 */
public class BlockingQueueDemo {
    public static void main(String[] args) throws InterruptedException {
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3); // 一直阻塞
        System.out.println(blockingQueue.size());
         blockingQueue.offer("a");
         blockingQueue.offer("b");
        System.out.println(blockingQueue.size());
        blockingQueue.offer("c");
        System.out.println(blockingQueue.size());
         blockingQueue.offer("d",3L, TimeUnit.SECONDS);
        System.out.println(blockingQueue.size());
         // 等待3秒超时退出
        System.out.println(blockingQueue.poll()); // a
        System.out.println(blockingQueue.poll()); // b

        System.out.println(blockingQueue.poll()); // c

        System.out.println(blockingQueue.poll(3L,TimeUnit.SECONDS)); // 阻塞
    }
}
